package xyz.xtt.asset.entity;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.StringUtils;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import xyz.xtt.asset.api.enums.BizTypeEnum;
import xyz.xtt.asset.api.enums.ShowStateEnum;
import xyz.xtt.asset.enums.PayUnitEnum;
import xyz.xtt.asset.utils.BigDecimalUtils;

/**
 * <p>
 * 资产基础信息表
 * </p>
 *
 * @author author
 * @since 2024-10-31
 */
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Data
@Accessors(chain = true)
@TableName("tbl_asset")
@ApiModel(value = "TblAsset对象", description = "资产基础信息表")
public class TblAsset implements Serializable {

	private static final long serialVersionUID = 1L;

	@ApiModelProperty(value = "主键id")
	@TableId(value = "id", type = IdType.AUTO)
	private Long id;

	@ApiModelProperty(value = "父id 归属地皮类是0，经营证类归属的地皮")
	private String parentId;

	@ApiModelProperty(value = "资产编号")
	private Long assetNo;

	@ApiModelProperty(value = "资产名称")
	private String assetName;

	@ApiModelProperty(value = "资产描述")
	private String description;

	@ApiModelProperty(value = "指导价")
	private BigDecimal guidePrice;
	
	@Deprecated
	@ApiModelProperty(value = "资产价格")
	private BigDecimal price;

	@ApiModelProperty(value = "资产计价单位")
	private Integer unit;

	@ApiModelProperty(value = "资产数量")
	private Integer amount;

	@ApiModelProperty(value = "创作者ID 平台方")
	private String authorId;

	@ApiModelProperty(value = "所有人ID 拥有者")
	private String ownerId;

	@ApiModelProperty(value = "合作者ID 代理商")
	private String cooperationId;

	@ApiModelProperty(value = "资产热度")
	private Long popularityRate;

	@ApiModelProperty(value = "资产一级分00：地皮，01：经营证，02：店铺，03：社群，04：权益，05：藏品，06：元宇宙；")
	private Integer firstType;

	@ApiModelProperty(value = "资产二级分类00：商业地皮，      其他类型依次类推：01、02、……10：A类经营证，20：实体店铺,30：商家社群,40：商家权益，50：音乐，60：数字人，")
	private Integer secondType;

	@ApiModelProperty(value = "资产状态0：销毁，1：冻结，2：上架，3：下架")
	private Integer showState;
	
	@Deprecated
	@ApiModelProperty(value = "出售方式0:出售1：寄售2：赠送3：拍卖")
	private Integer sellPattern;

	@ApiModelProperty(value = "创造时间")
	private Long createDatetime;

	@ApiModelProperty(value = "最新修改时间")
	private Long lastModifyDatetime;

	@ApiModelProperty(value = "业务方商品id")
	private String goodsId;

	@ApiModelProperty(value = "源数据")
	private String metaData;
	
	@ApiModelProperty(value = "风格id")
	private Long collectionId;
	
	private String bizType;
	/**
	 * 归属方 0用户1平台2代理商
	 */
	private Integer ownerType;
	/**
	 * 热度
	 */
	private BigDecimal popularity;
	
	@ApiModelProperty(value = "是否可售卖")
	private Integer sellable;
	@ApiModelProperty(value = "是否可赠送")
	private Integer giftable;
	@ApiModelProperty(value = "是否可参与排行榜")
	private Integer rankable;

	@ApiModelProperty(value = "根资产id")
	private Long rootAssetId;

	public boolean isUpdate() {
		return StringUtils.isBlank(ownerId) && showState == ShowStateEnum.DOWN.code();
	}

	public boolean filter() {
		return StringUtils.isNotBlank(ownerId) && Objects.nonNull(ownerType) && Objects.nonNull(PayUnitEnum.queryConvert(unit))
				&& BigDecimalUtils.isGreaterThanZero(price) && Objects.nonNull(showState);
	}

	/**
	 * 是否是中资产中心的数据
	 *
	 * @param uid
	 * @return
	 */
	public boolean isAssetNft() {
		return Objects.equals(BizTypeEnum.NFT.dbType(), bizType);
	}

	public boolean isMiner(String uid) {
		return Objects.equals(ownerId, authorId) && Objects.equals(uid, authorId);
	}

	/**
	 * 限制资产铸造完成一段时间内不允许上架
	 */
	public boolean checkCanOnDate(Long durationMinutesLimit) {
		return System.currentTimeMillis() - createDatetime >= TimeUnit.MINUTES.toMillis(durationMinutesLimit);
	}
	
	public boolean isTopTrade(String sellerId) {
		return Objects.equals(rootAssetId, id)
				&& Objects.equals(authorId, sellerId);
	}
}
